//
//  ZLTableView.m
//  ZLTableView
//
//  Created by 赵振磊 on 2022/3/19.
//

#import "ZLTableView.h"
#import <Masonry/Masonry.h>

@interface ZLTableView()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableview;
@end

@implementation ZLTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    if (self = [super initWithFrame:frame]) {
        [self setupTable:style];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupTable:UITableViewStyleGrouped];
    }
    return self;
}

- (void)setupTable:(UITableViewStyle)style {
    self.tableview = [[UITableView alloc] initWithFrame:CGRectZero style:style];
    self.tableview.backgroundColor = [UIColor clearColor];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    if (@available(iOS 15.0, *)) {
        self.tableview.sectionHeaderTopPadding = 0;
    }
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
}

- (void)registerCell:(NSArray<NSString *> *)cellNameArray {
    for (NSString *cellName in cellNameArray) {
        Class cellClass = NSClassFromString(cellName);
        if (cellClass) {
            [self.tableview registerClass:cellClass forCellReuseIdentifier:cellName];
        }
    }
}

- (void)registerCellNib:(NSArray<NSString *> *)nibNameArray {
    for (NSString *nilName in nibNameArray) {
        if (nilName.length > 0) {
            UINib *nib = [UINib nibWithNibName:nilName bundle:[NSBundle mainBundle]];
            if (nib) {
                [self.tableview registerNib:nib forCellReuseIdentifier:nilName];
            }
        }
    }
}

- (void)registerHeaderFooter:(NSString *)viewClass {
    Class class = NSClassFromString(viewClass);
    if (!class) {
        return;
    }
    [self.tableview registerClass:[class class] forHeaderFooterViewReuseIdentifier:viewClass];
}

- (void)registerHeaderFooterNib:(NSString *)nibViewClass {
    if (nibViewClass.length == 0) {
        return;
    }
    UINib *nib = [UINib nibWithNibName:nibViewClass bundle:[NSBundle mainBundle]];
    if (!nib) {
        return;
    }
    [self.tableview registerNib:nib forCellReuseIdentifier:nibViewClass];
}

- (void)sectionNumber:(ZLSectionNumber)sectionNumber
            rowNumber:(ZLRowNumber)rowNumber
           cellHeight:(ZLCellHeight)cellHeight
        indexPathCell:(ZLIndexPathCell)indexPathCell
           selectCell:(ZLSelectCell)selectCell {
    
    self.sectionNumber = sectionNumber;
    self.rowNumber = rowNumber;
    self.cellHeight = cellHeight;
    self.indexPathCell = indexPathCell;
    self.selectCell = selectCell;
}

- (void)sectionHeaderHeight:(ZLSectionHeaderFooterHeight)sectionHeaderHeight
          sectionHeaderView:(ZLSectionHeaderFooter)sectionHeaderView {
    self.sectionHeaderHeight = sectionHeaderHeight;
    self.sectionHeader = sectionHeaderView;
}
- (void)sectionFooterHeight:(ZLSectionHeaderFooterHeight)sectionFooterHeight
          sectionFooterView:(ZLSectionHeaderFooter)sectionFooterView {
    self.sectionFooterHeight = sectionFooterHeight;
    self.sectionFooter = sectionFooterView;
}

- (void)editWidthConfigs:(NSArray<ZLTableViewEditConfig *> *)configs {
    self.editConfigs = configs;
    [self.tableview reloadData];
}

- (void)reloadData {
    [self.tableview reloadData];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (self.sectionNumber) {
        return self.sectionNumber();
    }
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.rowNumber) {
        return self.rowNumber(section);
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.cellHeight) {
        return self.cellHeight(indexPath);
    }
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.indexPathCell) {
        return self.indexPathCell(tableView, indexPath);
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (self.sectionHeaderHeight) {
        return self.sectionHeaderHeight(section);
    }
    return 0.1f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (self.sectionHeader) {
        return self.sectionHeader(section);
    }
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (self.sectionFooterHeight) {
        return self.sectionFooterHeight(section);
    }
    return 0.1f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (self.sectionFooter) {
        return self.sectionFooter(section);
    }
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.selectCell) {
        self.selectCell(indexPath);
    }
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.editConfigs.count;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSMutableArray *actions = [NSMutableArray array];
    for (ZLTableViewEditConfig *config in self.editConfigs) {
        UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:config.title handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
            if (config.action) {
                config.action(indexPath);
            }
        }];
        action.backgroundColor = config.backgroundColor ? : [UIColor redColor];
        [actions addObject:action];
    }
    return actions;
}

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    for (UIView * lview in cell.superview.subviews.firstObject.subviews) {
        for (UIView * subcaceView in lview.subviews) {
            if ([subcaceView isKindOfClass:NSClassFromString(@"UIButtonLabel")]) {
                NSString *text = [subcaceView valueForKey:@"text"];
                for (ZLTableViewEditConfig *config in self.editConfigs) {
                    if ([config.title isEqualToString:text] && config.titleColor) {
                        [subcaceView setValue:config.titleColor forKey:@"textColor"];
                    }
                }
            }
        }
    }
}
@end
